package com.yksj.healthtalk.ui.setting;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.open.share.OpenManager;
import com.open.share.utils.SharedPreferenceUtil;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 解除绑定微博
 * @author Administrator
 *
 */
public class SettingSinaWeiBoBound extends BaseFragmentActivity implements OnClickListener {
	
	private TextView mInputPsd;//用户绑定的sina  UID
	private PopupWindow mWindow;
	private CustomerInfoEntity mEntity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_sina_weibo);
		initTitle();
		init();
	}
	
	private void init() {
		titleTextV.setText("绑定微博");
		mEntity = SmartFoxClient.getSmartFoxClient().getCustomerInfoEntity();
		titleLeftBtn.setOnClickListener(this);
		mInputPsd = (TextView)findViewById(R.id.setting_input_psd);
		mInputPsd.setText(mEntity.getMicroBlogCode());
		findViewById(R.id.next).setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
			break;
		case R.id.queren:
			showPop(v);
			unBindSina();
			break;
		case R.id.next:
		case R.id.cancle:
			showPop(v);
			break;
		}
	}
	
	/**
	 * 解除微博绑定
	 */
	private void unBindSina() {
		
		CustomerInfoEntity entity=SmartFoxClient.getLoginUserInfo();
		HttpRestClient.doHttpUnBindSina(SmartFoxClient.getLoginUserId(),entity.getMicroBlogUID(),
				new AsyncHttpResponseHandler(this){
		@Override
		public void onSuccess(int statusCode, String content) {
			super.onSuccess(statusCode, content);
			ToastUtil.showToastPanl("已解除绑定");
			SharedPreferenceUtil.clear(getApplicationContext(), OpenManager.getInstatnce().getSpName(OpenManager.SINA_WEIBO));
			SmartFoxClient.getSmartFoxClient().getCustomerInfoEntity().setSetBindSinaState("0");
			Intent intent =getIntent();
			intent.putExtra("isBoundSina", true);
			setResult(RESULT_OK, intent);
			finish();
		}
	});
	}

	/**
	 * 是否解除绑定pop
	 */
	private void showPop(View view){
		if(mWindow==null){
			View inflate = LayoutInflater.from(this).inflate(R.layout.photo_delete_pop, null);
			TextView textView=(TextView) inflate.findViewById(R.id.mtext);
			textView.setText("你确定要解除微博绑定吗?");
			inflate.findViewById(R.id.queren).setOnClickListener(this);
			inflate.findViewById(R.id.cancle).setOnClickListener(this);
			mWindow=new PopupWindow(inflate,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
			mWindow.setAnimationStyle(R.style.AnimationPreview);
			mWindow.setBackgroundDrawable(new BitmapDrawable());
			mWindow.setFocusable(true);
		}else if(mWindow.isShowing()){
			mWindow.dismiss();
			return ;
		}
		mWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
	}
	
}
